package com.xzz.dfs;

/**
 * @author: hhz
 * @create: 2022-01-24 14:32
 * 1020. 飞地的数量
 **/
public class NumEnclaves {
    public static void main(String[] args) {
        System.out.println(numEnclaves(new int[][]{{0,0,0,0},{1,0,1,0},{0,1,1,0},{0,0,0,0}}));
    }
    public static int numEnclaves(int[][] grid) {
        int n =grid.length;
        int m = grid[0].length;
        for (int i = 0; i < n; i++) {
            dfs(grid,i,0);
            dfs(grid,i,m-1);
        }
        for (int j = 0; j < m; j++) {
            dfs(grid,0,j);
            dfs(grid,n-1,j);
        }
        int res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(grid[i][j]==1){
                    res++;
                }
            }
        }
        return res;
    }
    public static void dfs(int[][] grid,int i,int j) {
        if(i<0 || j<0 || i>=grid.length || j>=grid[0].length){
            return;
        }
        if(grid[i][j]==0){
            return;
        }
        grid[i][j]=0;
        dfs(grid,i-1,j);
        dfs(grid,i+1,j);
        dfs(grid,i,j-1);
        dfs(grid,i,j+1);

    }
}
